<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>医院门诊系统注册页面</title>
  <link href="css/register.css" rel="stylesheet">
</head>
<body>

<div class="form-div">
  <div class="reg-content">
    <h1>欢迎注册</h1>
    <span>已有帐号？</span> <a href="patientLogin.html">登录</a>
  </div>
  <form id="reg-form">
    <table>
      <tr>
        <td>手机号</td>
        <td class="inputs">
          <input name="username" type="text" id="username" placeholder="请输入手机号">
          <br>
          <span id="username_err" class="err_msg" style="display: none">手机号格式有误</span>
        </td>
      </tr>
      <tr>
        <td>密码</td>
        <td class="inputs">
          <input name="password" type="password" id="password" placeholder="密码由6~12个字母和数字组成">
          <br>
          <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
        </td>
      </tr>
    </table>
    <div class="buttons">
      <button class="button" type="submit">注册</button>&nbsp;&nbsp;&nbsp;
    </div>
    <br class="clear">
  </form>

</div>

<script src="js/axios-0.18.0.js"></script>

<script>
  document.getElementById("reg-form").addEventListener("submit",function (e){
    e.preventDefault();
    patientRegister();
  });
  function patientRegister(){
    var user = {};

    user.userName = usernameInput.value;
    user.passWord = passwordInput.value;

    if(!regUserName(user.userName)){
      alert("请输入正确的手机号码")
      return
    }
    if(!regPassWord(user.passWord)){
      alert("请输入正确的密码格式")
      return
    }
    user.identity = '1'
    axios({
      method:"post",
      url:"/user/register",
      data:user
    }).then(function (resp) {
      if(resp.data.code == 20011){
        alert(resp.data.msg)
        localStorage.setItem("token",resp.data.data)
        window.location.assign("patientLogin.html");
      }else if(resp.data.code == 20010){
        alert(resp.data.msg)
        window.location.assign("patientRegister.html");
      }
    })


  }

  function regUserName(username){
    username =usernameInput.value;
    var reg = /^1[0-9]{10}$/;
    var flag =reg.test(username);
    return flag;
  }
  function regPassWord(password){
    password =passwordInput.value;
    var reg = /^\w{6,12}$/;
    var flag =reg.test(password);
    return flag;
  }


  var usernameInput = document.getElementById("username");
  usernameInput.onblur = function (){
    var flag = regUserName(usernameInput.value)
    if(flag){
      document.getElementById("username_err").style.display="none";
    }else{
      document.getElementById("username_err").style.display='';
    }
  }
  var passwordInput = document.getElementById("password");
  passwordInput.onblur = function (){
    var flag = regPassWord(passwordInput.value)
    if(flag){
      document.getElementById("password_err").style.display="none";
    }else{
      document.getElementById("password_err").style.display='';
    }
  }



</script>
</body>
</html>